'use strict';

const utils = require('utils/utils.js') 

const Controller = require('egg').Controller;
 
class AdminController extends Controller {

  // 初始化，获取服务层的路径  
  AdminDataindex = this.ctx.service.admin.dataindex
  HomeDataindex = this.ctx.service.home.dataindex 

  // 后台部分
  // // 查询所有用户
  async searchAll() {
    const {app,ctx} = this;
    const userinfo = ctx.state.user
    let responseData = null;
    try {
      const data = await this.AdminDataindex.UserSelect()
      if (data.length) {
        responseData = {  msg:'查询成功!', data:data, code:200 }
      } else {
        responseData = { msg:'查询失败！当前没有任何用户' ,  code:204 }
      }
    } catch (err) {
      responseData = { msg:"出现异常！",  error:err, code:500  }
    }
    ctx.body = responseData
  }

  // // 查询注销的用户
  async searchDelAll() {
    const {app,ctx} = this;
    const userinfo = ctx.state.user
    let responseData = null;
    try { 
      const paramsObj = { 
        isDelete:'1',
      } 
      // 查询是否存在当前用户
      const data = await this.AdminDataindex.UserSelect(null,paramsObj)  
      if (data.length ) {
        // 不允许展示密码
        responseData = {  msg:'查询注销用户成功!', data:data, code:200 }
      } else {
        responseData = { msg:'查询成功', error:'用户名认证出错！',  code:403 }
      }
    } catch (err) {
      responseData = { msg:"出现异常！",  error:err, code:500  }
    }
    ctx.body = responseData
  }


  // // 查询正常的用户
  async searchUseAll() {
    const {app,ctx} = this;
    const userinfo = ctx.state.user
    let responseData = null;
    try {
      
      const paramsObj = {
        isDelete:'0'
      } 
      let data = await this.AdminDataindex.UserSelect(null,paramsObj)  
      if (data.length > 0) { 
        // 清除注销用户
        // data = data.map(item=>{ 
        //    if(item.isDelete != '1'){
        //       return item;
        //    }
        // }) 
        responseData = {  msg:'查询非注销用户成功!', data:data, code:200 }
      } else {
        responseData = { msg:'查询失败！当前没有任何用户' ,  code:204 }
      }
    } catch (err) {
      responseData = { msg:"出现异常！",  error:err, code:500  }
    }
    ctx.body = responseData
  }

  // // 用户详情
  async search() {
    const {app,ctx} = this;
    let {username} = ctx.query;
    let responseData = null;
    try { 
      // 通过服务层获取数据
      const paramsObj = { 
        username,
      }
      // 匹配的用户
      const data = await this.AdminDataindex.UserSelect(null,paramsObj) 
      if (data.length) { 
        responseData = {  msg:'查询成功!', data:data, code:200 }
      } else {
        responseData = { msg:'查询成功', error:'用户名认证出错！',  code:403 }
      }
    } catch (err) {
      responseData = { msg:"出现异常！",  error:err, code:500  }
    }
    ctx.body = responseData
  }


  

  //   前台部分
  // 用户详情
  async about() {
    const {app,ctx} = this;
    const userinfo = ctx.state.user
    let responseData = null;
    try {
      // 通过服务层获取数据
      const paramsObj = {
        id:userinfo.data.id,
        username:userinfo.data.username,
      }
      // 首个匹配的用户
      const data = await this.AdminDataindex.UserSelect(null,paramsObj)
       
      if (data.length) {
        const user = data[0] ; 
        responseData = {  msg:'查询成功!', data:user, code:200 }
      } else {
        responseData = { msg:'查询成功', error:'用户名认证出错！',  code:403 }
      }
    } catch (err) {
      responseData = { msg:"出现异常！",  error:err, code:500  }
    }
    ctx.body = responseData
  }

  // // 修改信息
  async update() {
    const {ctx, app} = this;
    const userinfo = ctx.state.user
    const req = ctx.request.body;

    // 空值验证
    const errArr = app.validator.validate({
      username: 'string',
      password: 'string',
      sex: 'string',
      address: 'string',
    }, req);

    if (errArr) {
      ctx.body = { error:JSON.stringify(errArr), code:403   }
      return;
    }

    let responseData = null;
    try { 
     // 通过服务层获取数据
      const paramsObj = {
        id:userinfo.data.id,
        username:userinfo.data.username,
      } 
      // 首个匹配的用户
      const data = await this.AdminDataindex.UserSelectFirst(null,paramsObj) 
      if (data){
          let user = data;
          //修改用户信息
          const new_fieldObj = {
            username:req.username,
            password:req.password,
            sex:req.sex,
            address:req.address,
            updateTime:utils.mysqlDateType(false),
          }
          const paramsObj = {
            id:user.id
          } 
          const res = await this.AdminDataindex.UserUpdate(new_fieldObj,paramsObj)
          responseData = {  msg:'修改成功!',  code:204 }
      } else {
        responseData = { msg:'修改失败！当前的昵称已经被注册',  code:403 }
      }
    } catch (err) {
      responseData = { msg:"出现异常！",  error:err, code:500  }
    }
    ctx.body = responseData
  }
}
module.exports = AdminController;